import concurrent
import time
from concurrent.futures import ThreadPoolExecutor
import urllib.request

# def wait_on_b():
#     time.sleep(5)
#     print(b.result())
#     return 5
#
#
# def wait_on_a():
#     time.sleep(5)
#     print(a.result())
#     return 6
#
#
# executor = ThreadPoolExecutor(max_workers=2)
# a = executor.submit(wait_on_b)
# b = executor.submit(wait_on_a)


# def wait_on_future():
#     f = executor.submit(pow)
#     print(f.result())
#
# executor = ThreadPoolExecutor(max_workers=1)
# executor.submit(wait_on_future)

urls = [
    'https://www.baidu.com',
    'https://www.jd.com',
    'https://www.taobao.com',
    'https://www.ai.com',
    'https://www.bilibili.com/'
]


def load_url(url, timeout):
    with urllib.request.urlopen(url, timeout=timeout) as conn:
        return conn.read()


with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
    future_to_url = {executor.submit(load_url, url, 60): url for url in urls}
    for future in concurrent.futures.as_completed(future_to_url):
        url = future_to_url[future]
        try:
            data = future.result()
        except Exception as exc:
            print('%r generated an exception: %s' % (url, exc))
        else:
            print('%r result is %r' % (url, data))
